package com.biomatch.controller;

import com.biomatch.domain.Group;
import com.biomatch.domain.Group;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.Pagination;
import com.biomatch.response.RestResponse;
import com.biomatch.response.RestResult;
import com.biomatch.service.GroupService;
import com.biomatch.service.GroupService;
import com.biomatch.service.UserService;
import com.biomatch.utils.Internationalize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("lock.group")
@RequestMapping("/group")
@Api(value="组管理")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private Internationalize internationalize;


    @RequestMapping(value="/get4Page", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取组", response = Group.class, httpMethod = "POST")
    public RestResult<Group> get4Page(@RequestBody Pagination pagination) {
        return RestResponse.success(groupService.get4Page(pagination));
    }

    @RequestMapping(value="/getAll", method = RequestMethod.POST)
    @ApiOperation(value = "获取全部组", response = Group.class, httpMethod = "POST")
    public RestResult<List<Group>> getAll() {
        return RestResponse.success(groupService.getAll());
    }




    @RequestMapping(value="/saveOrUpdateGroup", method = RequestMethod.POST)
    @ApiOperation(value = "新增或更新组及组员", response = Group.class, httpMethod = "POST")
    public RestResult<Group> saveOrUpdate(@RequestBody Group group) {
        Group result = groupService.saveOrUpdateGroup(group);
        if (result == null)
        {
            return RestResponse.error(internationalize.getMessage("group.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }
    @RequestMapping(value="/deleteGroup", method = RequestMethod.POST)
    @ApiOperation(value = "删除组", response = Group.class, httpMethod = "POST")
    public RestResult<Group> deleteGroup(@RequestBody Group group) {
        Group result = groupService.deleteGroupById(group);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("group.status.used"));
        } else {
            return RestResponse.success(result);
        }
    }





}
